package com.nameless.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nameless.controller.feign.BrandFeign;
import com.nameless.controller.feign.MaintainRecordFeign;
import com.nameless.controller.feign.MaintainShopFeign;
import com.nameless.controller.feign.ShopFeign;
import com.nameless.dao.CarTypeMapper;
import com.nameless.entity.*;
import com.nameless.service.BrandService;
import com.nameless.service.CarService;
import com.nameless.vo.CarVo;
import com.nameless.vo.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@Api
@SuppressWarnings("all")
@RequestMapping("/cars/car")
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private BrandFeign brandFeign;
    @Autowired
    private ShopFeign shopFeign;
    @Autowired
    private MaintainShopFeign maintainShopFeign;
    @Autowired
    private MaintainRecordFeign maintainRecordFeign;
    @Autowired
    private CarTypeMapper carTypeMapper;

    @GetMapping("/carSort/{shopId}/{type}/{value}")
    public List<Car> carSort(@PathVariable Integer shopId, @PathVariable Integer type,@PathVariable Integer value){
        List<Car> cars = carService.carSort(shopId, type, value);
        for (Car car : cars) {
            car.setTypeName(carTypeMapper.selectById(car.getType()).getTypeName());
        }
        return cars;
    }

    @GetMapping("/carConfig/{shopId}/{type}/{value}")
    public List<Car> carConfig(@PathVariable Integer shopId, @PathVariable Integer type,@PathVariable Integer value){
        List<Car> cars = carService.carConfig(shopId, type, value);
        for (Car car : cars) {
            car.setTypeName(carTypeMapper.selectById(car.getType()).getTypeName());
        }
        return cars;
    }

    @GetMapping("/carBrand/{shopId}/{type}/{value}")
    public List<Car> carBrand(@PathVariable Integer shopId, @PathVariable Integer type,@PathVariable Integer value){
        List<Car> cars = carService.carBrand(shopId, type, value);
        for (Car car : cars) {
            car.setTypeName(carTypeMapper.selectById(car.getType()).getTypeName());
        }
        return cars;
    }
    //通过商店id和车型id进行查询
    @GetMapping("/carShopIdTypeId/{shopId}/{type}")
    public List<Car> carShopIdTypeId(@PathVariable Integer shopId, @PathVariable Integer type){
        List<Car> cars = carService.carShopIdTypeId(shopId, type);
        for (Car car : cars) {
            car.setTypeName(carTypeMapper.selectById(car.getType()).getTypeName());
        }
        System.out.println("--------------"+cars);
        return cars;
    }

    //展示所有
    @PostMapping("/typeAll")
    public Result selectList(@RequestBody CarVo carVo) {
        return carService.queryAllCars(carVo);
    }

    @GetMapping("/types")
    public Result selectList() {
        return carService.queryAlls();
    }

    @GetMapping("/selectId/{id}")
    public Car selectId(@PathVariable Integer id) {
        return carService.getById(id);
    }

    @GetMapping("/selectName/{name}")
    public Car selectId(@PathVariable String name) {
        return carService.getByName(name);
    }

    //    修改
    @PostMapping("/update")
    public Result update(@RequestBody Car car) {
        return carService.updates(car);
    }
    //    添加
    @PostMapping("/insert")
    public Result insert(@RequestBody Car car) {
        return carService.insert(car);
    }

    //    删除
    @DeleteMapping("/del")
    public Result del(Integer id) {
        return carService.delete(id);
    }


    @GetMapping("/getCarByShopId/{shopId}")
    public List<Car> getCarByShopId(@PathVariable Integer shopId) {
        LambdaQueryWrapper<Car> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Car::getShopId, shopId);
        List<Car> cars = carService.list(queryWrapper);
        for (Car car : cars) {
            CarType carType = carTypeMapper.selectById(car.getType());
            car.setTypeName(carType.getTypeName());
        }
        return cars;
    }

    @Autowired
    private BrandService brandService;

    @GetMapping("/getBrandByShopId/{shopId}")
    public List<Brand> getBrandByShopId(@PathVariable Integer shopId) {
        LambdaQueryWrapper<Car> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Car::getShopId, shopId);
        List<Car> list = carService.list(queryWrapper);
        return list.stream().map(item -> {
            Integer brandId = item.getBrandId();
            return brandService.getById(brandId);
        }).distinct().collect(Collectors.toList());
    }


    @GetMapping("/list")
    public Result list() {
        List<Brand> all = brandFeign.findAlls();
        return new Result(200,"查询成功",all);
    }
    @GetMapping("/listShop")
    public Result listShop() {
        List<Shop> all = shopFeign.findAll();
        return new Result(200,"查询销售铺成功",all);
    }
    @GetMapping("/listTainShop")
    public Result listtainShop() {
        List<MaintainShop> all = maintainShopFeign.findAll();
        return new Result(200,"查询维修店铺成功",all);
    }
    @PostMapping("/listTainRecord")
    public Result listTainRecord(@RequestBody MaintainRecord maintainRecord) {
        int all = maintainRecordFeign.insert(maintainRecord);
        return new Result(200,"添加维修记录成功",all);
    }


}
